package co.fitstart.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Page;

import co.fitstart.admin.dao.DistributorDao;
import co.fitstart.admin.entity.search.DistributorSearch;
import co.fitstart.admin.service.DistributorService;
import co.fitstart.entity.user.distributor.DistributionRecord;
import co.fitstart.entity.user.distributor.Distributor;
import co.fitstart.entity.user.distributor.DistributorUserRelation;

/**
 * 
 * @author mohanlan
 *
 */
@Service("distributorService")
public class DistributorServiceImpl implements DistributorService {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Dynamic
    private DistributorDao distributorDao;

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributorService#list(com.demo2do.core.support.Page)
     */
    public List<Distributor> list(Page page) {
        
        List<Distributor> result = distributorDao.listDistributor(page.getBeginIndex(), page.getMaxResultRecords());
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributorService#list()
     */
    public List<Distributor> list() {
        return distributorDao.listDistributor();
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributorService#search(co.fitstart.admin.entity.search.DistributorSearch, com.demo2do.core.support.Page)
     */
    @SuppressWarnings("unchecked")
    public List<Distributor> search(DistributorSearch search, Page page) {
        
        String sentence = search.buildSentenceTemplate();
        List<Distributor> result = genericDaoSupport.searchForList(sentence, search.buildParameters(), page.getBeginIndex(), page.getMaxResultRecords());
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributorService#loadByUserId(java.lang.Long)
     */
    public Distributor loadByUserId(Long userId) {
        return distributorDao.loadDistributor(userId);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributorService#load(java.lang.Long)
     */
    public Distributor load(Long id) {
        return genericDaoSupport.load(Distributor.class, id);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributorService#editDistributor(co.fitstart.entity.user.distributor.Distributor)
     */
    public void editDistributor(Distributor distributor) {
        genericDaoSupport.update(distributor);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributorService#createDistributor(co.fitstart.entity.user.distributor.Distributor)
     */
    public void createDistributor(Distributor distributor) {
        genericDaoSupport.save(distributor.init());
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributorService#deleteDistributor(co.fitstart.entity.user.distributor.Distributor)
     */
    public void deleteDistributor(Distributor distributor) {
        
        // 1. disable distributor
        genericDaoSupport.update(distributor.disable());
        
        // 2. disable relation
        distributorDao.deleteRelation(distributor);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributorService#listRelation(co.fitstart.entity.user.distributor.Distributor)
     */
    @SuppressWarnings("unchecked")
    public List<DistributorUserRelation> listRelation(Distributor distributor) {
        return genericDaoSupport.searchForList("FROM DistributorUserRelation WHERE distributor = :distributor AND disabled = false AND consumed = true AND order IS NOT NULL", "distributor", distributor);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributorService#payDistribution(co.fitstart.entity.user.distributor.Distributor, co.fitstart.entity.user.distributor.DistributionRecord)
     */
    public void payDistribution(Distributor distributor, DistributionRecord distributionRecord) {
        
        // 1. pay 
        genericDaoSupport.update(distributor.pay(distributionRecord.getOffer()));
        
        // 2. update status
        genericDaoSupport.update(distributionRecord.pay());
    }
}
